package com.chenwenxuan.sync.utils;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@SuppressWarnings("deprecation")
public class QueryString {

	private Map<String, List<String>> parameters;

	public QueryString(String qs) {
		parameters = new TreeMap<String, List<String>>();

		// Parse query string
		String pairs[] = qs.split("&");
		for (String pair : pairs) {
			String name;
			String value;
			int pos = pair.indexOf('=');
			// for "n=", the value is "", for "n", the value is null
			if (pos == -1) {
				name = pair;
				value = null;
			} else {

				name = URLDecoder.decode(pair.substring(0, pos));
				value = URLDecoder.decode(pair.substring(pos + 1, pair.length()));

			}
			List<String> list = parameters.get(name);
			if (list == null) {
				list = new ArrayList<String>();
				parameters.put(name, list);
			}
			list.add(value);
		}
	}

	public String getParameter(String name) {
		List<String> values = parameters.get(name);
		if (values == null)
			return null;

		if (values.size() == 0)
			return "";

		return values.get(0);
	}

	public String[] getParameterValues(String name) {
		List<String> values = parameters.get(name);
		if (values == null)
			return null;

		return (String[]) values.toArray(new String[values.size()]);
	}

	public Enumeration<String> getParameterNames() {
		return Collections.enumeration(parameters.keySet());
	}
	
	public Map<String, String[]> getParameterMap() {
		Map<String, String[]> map = new TreeMap<String, String[]>();
		for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
			List<String> list = entry.getValue();
			String[] values;
			if (list == null)
				values = null;
			else
				values = (String[]) list.toArray(new String[list.size()]);
			map.put(entry.getKey(), values);
		}
		return map;
	}
	
	public void printParameter() {
		for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
			List<String> list = entry.getValue();
			String[] values;
			if (list == null)
				values = null;
			else
				values = (String[]) list.toArray(new String[list.size()]);
			System.out.print(entry.getKey() + ": [");
			for ( String value : values) {
				System.out.print(value + ",");
			}
			System.out.println("]");
		}
	}
	
	
}